//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include "CDeviceEventDispatcher.h"
#include "_CDeviceEventDispatcher.cpp"

typedef ECode (*EventHandlerFn)(const MemoryBuf&);
typedef ECode (*EventHandlerMd)(PVoid, const MemoryBuf&);

ECode CDeviceEventDispatcher::InvokeEventHandler(
    /* [in] */ const MemoryBuf & ebEventData)
{
    PVoid pvThis, pvFunc;
    ECode ec;
    pvThis = m_pDelegate->GetThisPtr();
    pvFunc = m_pDelegate->GetFuncPtr();
    if (!pvThis) {
        ec= ((EventHandlerFn)pvFunc)(ebEventData);
    }
    else {
        ec= ((EventHandlerMd)pvFunc)(pvThis, ebEventData);
    }
    return ec;
}

ECode CDeviceEventDispatcher::SetEventHandler(
    /* [in] */ Address thisPtr,
    /* [in] */ Address function)
{
    m_pDelegate = new EventHandler(EventHandler::Make((PVoid)thisPtr, (PVoid)function));
    if (NULL == m_pDelegate) return E_OUT_OF_MEMORY;
    return NOERROR;
}
